<%--
  Created by IntelliJ IDEA.
  User: liuziyang
  Date: 2018/9/2
  Time: 12:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>index</title>
    <script type="text/javascript">
        window.onload = function () {
            //1.获取a节点，并为其添加onclick响应函数
            document.getElementsByTagName("a")[0].onclick = function () {
                //3.创建一个XMLHttpRequest对象
                var requset = new XMLHttpRequest();
                //4.准备发送请求的数据：url
                var url = this.href + "?time=" + new Date();

                // var method = "GET";
                var method = "POST";

                //5.调用XMLHttpRequest对象的open方法
                requset.open(method, url);
                requset.setRequestHeader("ContentType", "application/x-www-form-urlencoded");

                //6.调用XMLHttpRequest对象的send方法
                // requset.send(null);
                requset.send("test message");

                //7.为XMLHttpRequest对象添加onreadystatechange响应函数
                requset.onreadystatechange = function () {
                    //8.判断响应是否完成:XMLHttpRequest对象的readyState的属性值为4的时候
                    if (requset.readyState === 4) {
                        //9.再判断响应是否可用：XMLHttpRequest对象status属性值为200
                        if (requset.status === 200 || requset.status === 304) {
                            //10.打印响应结果：responseText
                            alert(requset.responseText);
                        }
                    }
                };

                //2.取消a节点的默认行为
                return false;
            }
        }
    </script>
</head>
<body>
<h4>Welcome to ajax</h4>
<a href="helloAjax.ini">hello ajax</a>
</body>
</html>
